﻿using System.Xml.Serialization;
using System.Collections.Generic;

namespace Bizspoke.BizTalk.Adapter
{
    public class MessageInfo
    {
        private ContextInfo contextInfo = new ContextInfo();
        private PartInfo partInfo = new PartInfo();
        private string messageInstanceId;

        public ContextInfo ContextInfo
        {
            get { return contextInfo; }
            set { contextInfo = value; }
        }

        public PartInfo PartInfo
        {
            get { return partInfo; }
            set { partInfo = value; }
        }

        public string MessageInstanceId
        {
            get { return messageInstanceId; }
            set
            {
                messageInstanceId = value;
                ContextInfo.MessageInstanceId = messageInstanceId;
                PartInfo.MessageInstanceId = messageInstanceId;
            }
        }

        public MessageInfo()
        {
        }
    }

    [XmlRoot]
    public class ContextInfo
    {
        [XmlAttribute]
        public int PropertiesCount;
        
        [XmlElement("Property")]
        public List<Property> Properties = new List<Property>();

        [XmlIgnore]
        public string MessageInstanceId;

        public ContextInfo()
        {
        }

        public Property this[string name]
        {
            get
            {
                return Find(name);
            }
        }

        private Property Find(string name)
        {
            return Properties.Find(delegate(Property property) { return property.Name == name; });
        }
    }

    [XmlRoot]
    public class PartInfo
    {
        private string messageInstanceId;

        [XmlAttribute]
        public int PartsCount;

        [XmlElement("MessagePart")]
        public List<MessagePart> MessageParts = new List<MessagePart>();

        [XmlIgnore]
        public string MessageInstanceId
        {
            get
            {
                return messageInstanceId;
            }
            set
            {
                messageInstanceId = value;
                foreach (MessagePart part in MessageParts)
                {
                    part.MessageInstanceId = messageInstanceId;
                }
            }
        }

        public PartInfo()
        {
        }

        public MessagePart this[string name]
        {
            get
            {
                return Find(name);
            }
        }

        private MessagePart Find(string name)
        {
            return MessageParts.Find(delegate(MessagePart part) { return part.Name == name; });
        }
    }

    [XmlRoot]
    public class Property
    {
        [XmlAttribute]
        public string Name;
        
        [XmlAttribute]
        public string Namespace;
        
        [XmlAttribute]
        public string Value;

        public Property()
        {
        }

        public override string ToString()
        {
            return Name + ": " + Value;
        }
    }

    [XmlRoot]
    public class MessagePart
    {
        [XmlAttribute]
        public string ID;
        
        [XmlAttribute]
        public string Name;
        
        [XmlAttribute]
        public string FileName;
        
        [XmlAttribute]
        public string Charset;
        
        [XmlAttribute]
        public string ContentType;

        [XmlIgnore]
        public string MessageInstanceId;

        public MessagePart()
        {
        }
    }
}
